package com.zmn.plat.common.util;

import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 类描述：数字字符串转数字数组
 *
 * @author yangshuangping
 * @since 2021/06/17 10:04
 */
public class IntegerStringToListUtil {
    /**
     * @param str 数字字符串
     * @return [123, 231, 274, 6785674567]
     * @author yangshuangping
     */
    public static List<Integer> tranStringToList(String str) {
        List<Integer> longList = Lists.newArrayList();
        // 对可能出现对特殊情况进行兜底判断
        if (str == null || StringUtils.isBlank(str) || str.equals(",")) {
            return longList;
        }
        return Optional.ofNullable(Arrays.stream(str.split(",", -1))
                .map(s -> Integer.parseInt(s.trim()))
                .collect(Collectors.toList())).orElse(Lists.newArrayList());
    }
}
